<?php
namespace Home\Controller;
use Think\Controller;
class PayController extends Controller {
	
	public function __construct() {
		parent::__construct();
		if(empty($_SESSION['user']['id']))
		{
			$this->redirect('Home/Login/login');
		}
    }
	
	/*public function __construct() {
		parent::__construct();
		if(empty($_SESSION['visitor_id']))
		{
			header('Location: http://container.open.taobao.com/container?appkey=23344775');
		}
    }*/
	
	
	public function index(){
		
		$paymethod=M('pay_method');
		$methodinfo=$paymethod->field('id,method')->limit(10)->select();
		$count['count']=$paymethod->count();
		$count['pageNo']=1;
		$count['pageSize']=10;
		$count['countpage']=ceil($count['count']/$count['pageSize']);
		$order['name']='id';
		$order['asc']='desc';
		
		$this->assign('methodinfo',$methodinfo);
		$this->assign("order",$order);
		$this->assign("count",$count);	
		$this->display();
    }
	
	public function method_find($pageNo,$pageSize,$method,$name,$asc){
		if(!empty($method)){
			$title=trim($method);
			$where['method'] = array('like',"%$method%");
			$data['method']=$method;
		}
		
		if(empty($name)){
			$name='id';
		}
		if(empty($asc)){
			$asc='desc';
		}
		
		$paymethod = M("pay_method"); 
		$count['count']=$paymethod->where($where)->count();
		$count['countpage']=ceil($count['count']/$pageSize);
		
		if($pageNo>=$count['countpage']){
			$pageNo=$count['countpage'];
		}
		if($pageNo<=0){
			$pageNo=1;
		}
		
		$methodinfo=$paymethod->where($where)->order("$name $asc")->limit(($pageNo-1)*$pageSize,$pageSize)->select(); 
		
		$count['pageSize']=$pageSize;
		$count['pageNo']=$pageNo;
		$count['num']=$count['countpage']-4;
		$order['name']=$name;
		$order['asc']=$asc;
		$this->assign("methodinfo",$methodinfo);
		$this->assign("data",$data);
		$this->assign("res",$res);
		$this->assign("count",$count);
		$this->display('index'); 
	}
	
	public function method_delete(){
		
		$id=empty($_REQUEST['id'])?0:$_REQUEST['id'];
		$paymethod=M('pay_method');
		$res=$paymethod->where('id='.$id)->delete();
		
		if($res)
		{
			$this->success('删除成功');
		}
		else
		{
			$this->error('删除失败');
		}	
    }
	
	public function method_info(){
		//var_dump($_REQUEST);die();
		$method=empty($_REQUEST['method'])?'':trim($_REQUEST['method']);
		$id=empty($_REQUEST['id'])?'':$_REQUEST['id'];
		
		if($id){
			$this->method_edit($method,$id);
		}else{
			$this->method_add($method);
		}
    }
	
	public function method_add($method){
		
		$method=empty($_REQUEST['method'])?'':$_REQUEST['method'];
		
		if(!empty($method))
		{
		    $paymethod=M('pay_method');
			$data['method']=$method;
			$res=$paymethod->add($data);
			if($res)
			{
				$this->success('添加成功');
			}
			else
			{
				$this->error('添加失败');
			}	
		}
    }
	
	public function method_edit($method,$id){
		$paymethod=M('pay_method');
		
		/*$method=empty($_REQUEST['method'])?'':$_REQUEST['method'];
		$id=empty($_REQUEST['id'])?'':$_REQUEST['id'];*/
		
		if(!empty($id))
		{
			$data['method']=$method;
			$where['id']=$id;
			$res=$paymethod->where($where)->save($data);
			if($res!==false)
			{
				$this->success('修改成功');
			}
			else
			{
				$this->success('修改失败');
			}
		}
    }
	
}
















